<script setup>
// #region 引入包, 函数和组件
import getInterface from "@/api";
import { ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router"; // 路由信息
import Photos from "./components/photos.vue"; // 轮播图
import Loading from "@/components/Loading2.vue"; // 加载中组件

// #endregion

// #region // 使用模块(仓库、路由、父组件参数函数)
const route = useRoute();
const router = useRouter();
// #endregion

// #region // 变量
const film = ref(null); // 电影数据
const showH3 = ref(false);
const showDes = ref(false); // 显示全部的描述
const des = ref(null); // 描述元素
const showSwiper = ref(false);
const initIndex = ref(0); // 初始显示轮播图下标
const showAll = ref(false); // 显示全部剧照
const loading = ref(false); // 显示加载中
// #endregion

// #region // 函数
const getFilm = async () => {
  try {
    loading.value = true;
    const res = await getInterface(
      {
        filmId: route.params.filmId,
      },
      "film.info"
    );
    // console.log(res.data.data.film)
    film.value = res.data.data.film;
    loading.value = false;
  } catch (error) {
    // console.log(error);
    loading.value = false;
  }
};
const onScroll = (e) => {
  const scrollTop = e.target.scrollTop;
  // console.log(scrollTop);
  showH3.value = scrollTop > 60;
};
// 显示全部的描述
const showDesFn = () => {
  showDes.value = !showDes.value;
  if (showDes.value) {
    des.value.style.cssText = "transition: none;overflow: auto;";
    const height = des.value.scrollHeight;
    des.value.style.cssText = "height:" + height + "px";
  } else {
    des.value.style.height = "0.38rem";
  }
};
// 点击显示轮播图
const showSwiperFn = (index) => {
  // console.log(index)
  initIndex.value = index;
  showSwiper.value = true;
};
// #endregion

// #region // 生命周期和vue函数
// console.log(route.path);
getFilm(); // 初次调用接口获取电影详情数据
// 计算属性: 上映时间
const premiereAt = computed(() => {
  if (film.value) {
    const date = new Date(film.value.premiereAt * 1000);
    return date.toLocaleDateString().replace(/\/\d+/g, ($0) => {
      return "-" + $0.slice(1).padStart(2, "0");
    });
  }
});
// #endregion
</script>

<template>
  <div class="filmdetail" @scroll="onScroll">
    <div class="head_img">
      <i @click="router.back">&lt;</i>
      <Transition
        class="animate__animated"
        enter-active-class="animate__fadeIn"
        leave-active-class="animate__fadeOut"
      >
        <h3 v-show="showH3">{{ film?.name }}</h3>
      </Transition>
      <img :src="film?.poster" />
    </div>
    <div class="film_detail">
      <h3>
        {{ film?.name }} <span>{{ film?.filmType.name }}</span
        ><em
          ><b>{{ film?.grade }}</b
          >分</em
        >
      </h3>
      <p>{{ film?.category.replace(/[|]/g, " | ") }}</p>
      <p>{{ premiereAt }}上映</p>
      <p>{{ film?.nation }} | {{ film?.runtime }}分钟</p>
      <h5 ref="des">{{ film?.synopsis }}</h5>
      <h4><i :class="{ rotate: showDes }" @click="showDesFn">&lt;</i></h4>
    </div>
    <div class="actors">
      <h3>演职人员</h3>
      <ul>
        <li v-for="(li, ind) in film?.actors" :key="ind">
          <img draggable="false" :src="li.avatarAddress" />
          <span>{{ li.name }}</span>
          <em>{{ li.role }}</em>
        </li>
      </ul>
    </div>
    <div class="actors photos">
      <h3>
        剧照 <span @click="showAll = true">全部({{ film?.photos.length }})<i>&gt;</i></span>
      </h3>
      <ul>
        <li @click="showSwiperFn(ind)" v-for="(img, ind) in film?.photos" :key="ind">
          <img draggable="false" :src="img" />
        </li>
      </ul>
    </div>
    <div class="all" v-show="showAll">
      <h3>
        <i @click="showAll = false">&lt;</i> <span>剧照 ({{ film?.photos.length }})</span>
      </h3>
      <ul>
        <li @click="showSwiperFn(ind)" v-for="(img, ind) in film?.photos" :key="ind">
          <img draggable="false" :src="img" />
        </li>
      </ul>
    </div>
    <footer @click="router.push({ name: 'filmcinemas', params: { filmId: route.params.filmId } })">
      选座购票
    </footer>
    <Photos
      :initIndex
      :list="film?.photos"
      v-if="showSwiper"
      v-model:showSwiper="showSwiper"
    ></Photos>
    <Loading :loading></Loading>
  </div>
</template>

<style lang="scss" scoped>
@mixin nowrap($size) {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: $size;
}
@mixin img($width, $height) {
  & img {
    width: $width;
    height: $height;
    object-fit: cover;
  }
}

.filmdetail {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  background: #f4f4f4;

  .head_img {
    position: relative;

    i {
      position: fixed;
      display: block;
      font-size: 0.24rem;
      transform: scale(0.8, 1.4);
      z-index: 2;
      width: calc(0.3rem / 0.8);
      height: calc(0.3rem / 1.4);
      line-height: 0.18rem;
      text-align: left;
      padding-left: 0.08rem;
      top: 0.12rem;
      border-radius: 50%;
      background: rgba($color: #fff, $alpha: 0.6);
      color: #666;
    }

    h3 {
      position: fixed;
      top: 0;
      left: 0;
      background: #fff;
      height: 0.45rem;
      border-bottom: 0.01rem solid #fbfbfb;
      width: 100%;
      line-height: 0.44rem;
      text-align: center;
      font-size: 0.17rem;
      z-index: 1;
    }

    @include img(100%, 2.1rem);
  }
}

h3 {
  color: var(--cl-blk);
}

.film_detail {
  padding: 0.12rem 0.15rem 0.15rem;
  margin-bottom: 0.1rem;
  background: #fff;

  h3 {
    display: flex;
    align-items: center;
    width: 100%;
    font-size: 0.18rem;
    height: 0.27rem;

    span {
      margin-left: 0.07rem;
      font-size: 0.09rem;
      width: 0.16rem;
      height: 0.14rem;
      background: #d2d6dc;
      color: #fff7ef;
      text-align: center;
      line-height: 0.14rem;
    }

    em {
      margin-left: auto;
      color: #ffb232;
      font-size: 0.11rem;

      b {
        font-size: 0.18rem;
        font-style: italic;
      }
    }
  }

  p {
    height: 0.195rem;
    margin-top: 0.04rem;
    font-size: 0.13rem;
    color: var(--cl-wh);
  }

  h5 {
    margin-top: 0.12rem;
    font-size: 0.13rem;
    color: var(--cl-wh);
    height: 0.38rem;
    overflow: hidden;
    transition: height 0.5s ease;
    line-height: 0.18rem;
  }

  h4 {
    height: 0.16rem;
    display: flex;
    justify-content: center;

    i {
      display: block;
      text-align: center;
      height: 0.16rem;
      width: 0.2rem;
      text-align: center;
      font-size: 0.12rem;
      transform: rotate(-90deg) scale(0.8, 1.4);
      color: #ccc;
      transition: transform 0.2s ease;
      transform-origin: 0.1rem 0.092rem;

      &.rotate {
        transform: rotate(90deg) scale(0.8, 1.4);
      }
    }
  }
}

.actors {
  height: 1.925rem;
  width: 100%;
  margin-bottom: 0.1rem;
  background: #fff;
  overflow: hidden;

  h3 {
    padding: 0.15rem;
    font-size: 0.16rem;
  }

  ul {
    display: flex;
    overflow: hidden;
    overflow-x: auto;
    padding-left: 0.15rem;
    color: var(--cl-blk);

    &::-webkit-scrollbar {
      display: none;
    }

    li {
      margin-right: 0.1rem;
      display: flex;
      flex-direction: column;
    }

    @include img(0.85rem, 0.85rem);

    span {
      width: 0.85rem;
      text-align: center;
      padding-top: 0.1rem;
      @include nowrap(0.12rem);
    }

    em {
      color: var(--cl-wh);
      text-align: center;
      @include nowrap(0.1rem);
      width: 0.85rem;
    }
  }
}

.photos {
  margin-bottom: 0.59rem;
  height: 1.65rem;

  h3 {
    display: flex;
    justify-content: space-between;

    span {
      font-size: 0.13rem;
      color: var(--cl-wh);
    }

    i {
      display: inline-block;
      transform: scale(0.7, 1.4);
      font-size: 0.14rem;
    }
  }

  ul li {
    @include img(1.5rem, 1rem);
  }
}

.all {
  position: fixed;
  background: #fff;
  width: 100%;
  height: 100%;
  inset: 0;
  z-index: 5;

  h3 {
    height: 0.44rem;
    font-size: 0.17rem;
    text-align: center;
    line-height: 0.44rem;
  }

  i {
    position: fixed;
    display: block;
    font-size: 0.24rem;
    transform: scale(0.8, 1.4);
    z-index: 2;
    width: 0.375rem;
    height: 0.2142857143rem;
    line-height: 0.18rem;
    text-align: left;
    padding-left: 0.08rem;
    top: 0.12rem;
    color: #666;
  }

  @include img(1.24rem, 1.24rem);

  ul {
    display: flex;
    flex-wrap: wrap;
  }

  li {
    margin: 0 0.01rem 0.01rem 0;
  }
}

footer {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 0.49rem;
  width: 100%;
  text-align: center;
  background-color: #ff5f16;
  color: #fff;
  font-size: 0.16rem;
  line-height: 0.49rem;
}
</style>
